@extends('admin.master')

@section('top')
<div class="top-breadcrumb pull-left">{!! Breadcrumbs::render('customer') !!}</div>
@endsection

@section('content')
<div class="section-search mb10">
{!! Form::model($where, ['method'=>'get', 'class'=>'form-inline']) !!}
	<div class="form-group">
		{!! Form::text('word', null, ['class'=>'form-control len', 'placeholder'=>'客户名称或域名']) !!}
	</div>
	<div id="department" class="inline-block">
		<div class="form-group">
			<label class="control-label">部门：</label>
			<select name="department_id" class="form-control autosize department"></select>
		</div>
		<div class="form-group">
			<label class="control-label">业务员：</label>
			<select name="saler_id" class="form-control autosize saler"></select>
		</div>
	</div>
	<button type="submit" class="btn btn-default">查询</button>
{!! Form::close() !!}
</div>
<div class="gridSection">
{!! Form::open(['url'=>'admin/user']) !!}
	<table class="table table-hover table-list" id="customer-list">
		<thead>
			<tr>
				<th width="10"><input type="checkbox" class="checkbox-all"></th>
				<th>客户名称</th>
				<th class="text-center">订单数量</th>
				<th>域名</th>
				<th>联系人</th>
				<th>电话</th>
				<th>业务员</th>
				<th>地区</th>
				<th>详细地址</th>
				<th class="text-right">操作</th>
			</tr>
		</thead>
		<tbody>
			@forelse ($data as $value)
				<tr data-id="{{ $value->id }}">
					<td><input type="checkbox" name="id[]" class="checkbox-item" value="{{ $value->id }}"></td>
					<td width="200">
						<a href="{{ url('admin/customer/'.$value->id) }}">{{ $value->name }}</a>
						@if ($value->vip == 1)
							<span class="label label-warning">VIP</span>
						@endif
					</td>
					<td class="text-center"><span class="text-success">{{ $value->order->count() }}</span></td>
					<td><a href="http://{{ $value->domain }}" target="_blank">{{ $value->domain }}</a></td>
					<td>{{ $value->contact_people }}</td>
					<td>{{ $value->phone }}</td>
					<td><span class="text-success">{{ $value->saler->name or '---' }}</span></td>
					<td>{{ $value->city->implode(' ') }}</td>
					<td>{{ $value->detail_address }}</td>
					<td class="text-right">
						<div class="toolbar">
							<a href="{{ route('admin.customer-note.index', ['cid'=>$value->id]) }}" class="btn-link">笔记</a>
							<span class="text-explode ">|</span>
						@if (auth()->user()->can('contract'))
							<a href="{{ route('admin.contract.index', ['cid'=>$value->id]) }}" class="btn-link">合同和发票</a>
							<span class="text-explode ">|</span>
						@endif
							<a href="{{ route('admin.customer.edit', $value['id']) }}" class="btn-link">修改</a>
						@if (auth()->user()->can('customer-delete'))
							<span class="text-explode ">|</span>	
							<a href="javascript:void(0);" data-id="{{ $value['id'] }}" class="btn-link delete-item">删除</a>
						@endif
						</div>
					</td>
				</tr>
			@empty
			@endforelse
		</tbody>
	</table>
	@if (!$data->isEmpty())
	<table class="table table-fixed table-footer">
		<tfoot>
			<tr>
				<td width="10"><input type="checkbox" class="checkbox-all"></td>
				<td colspan="11">
					<div class="pull-left">
						<div class="toolbar-btn">
							<a href="javascript:;" class="btn btn-default  selected-items btn-move disabled" action="{{ url('admin/customer/move') }}"><span>转移客户</span></a>
						</div>
					</div>
					<div class="pagination-box">
						<div class="pull-right">
							<div class="pagination-info">共有{{ $data->total() }}条， 每页显示：20条</div>
							{!! $data->appends(request()->except('page'))->render() !!}
						</div>
					</div>				
				</td>
			</tr>
		</tfoot>
	</table>
	@else
	<div class="simple-none-data text-center">
		<i class="fa fa-exclamation-circle text-size-20 text-muted" style="vertical-align: middle"></i> 
		<span class="text-size-14 inline-block ml5">没有查询到符合条件的记录</span>
	</div>
	@endif
{!! Form::close() !!}
</div>
@endsection

@section('foot')
<!-- Modal -->
<div class="modal fade dialog-form-class" id="dialog-move" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">客户转移</h4>
        </div>
        <div class="modal-body clearfix">
        	<div class="model-department">
        		{!! Form::open(['class'=>'form-horizontal', 'id'=>'customer-move']) !!}
        			<input type="hidden" id="customer-ids" name="ids">
					<div class="form-group">
						<div class="col-sm-12">
							<select class="form-control department"></select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-12">
							<select name="saler_id" class="form-control saler"></select>
						</div>
					</div>
					<div class="form-group">
					    <div class="col-sm-12">
							<div class="checkbox">
								<label>
									<input type="checkbox" name="order-move" value="1" checked> 
									<i class="text-success text-size-13">同时转移 订单 和 续费</i>
								</label>
							</div>
					    </div>
					</div>
				{!! Form::close() !!}
        	</div>
        </div>
        <div class="modal-footer">
            <a class="btn btn-primary btn-modal-ok">确定</a>
        	<button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
        </div>
    </div>
  </div>
</div>
<!-- Modal End -->
<script src="{{ url('js/department.js') }}"></script>
<script type="text/javascript">
	var departments = {!! json_encode($departmentsTree, JSON_UNESCAPED_UNICODE) !!};
	var salers = {!! json_encode($salers, JSON_UNESCAPED_UNICODE) !!};
	var where = {!! $where->toJson() !!};

	department.init({
		departments: departments,
		salers: salers,
		where: where,
		container: '#department'
	}).make();

	department.init({
		departments: departments,
		salers: salers,
		where: {'region_id': {{ $user->region_id }}},
		container: '.model-department'
	}).make();

	$('.btn-move').on('click', function (argument) {

		var count = $(".checkbox-item[type='checkbox']:checked").length;
		var ids = [];

		if (count == 0) {
			$('#dialog-tip').modal('show');
			return false;
		}

	    $('#customer-move').attr('action', $(this).attr('action'));

	    $(".checkbox-item[type='checkbox']:checked").each(function() {
	    	ids.push($(this).val());
	    });

	    $('#customer-ids').val(ids.join('-'));

	    $('#dialog-move').modal('show');

	    return false;
	});
</script>
@endsection